package com.ks.app.admin.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.ks.app.admin.hibernate.KsUser;
import com.ks.app.admin.service.UserService;
import com.ks.app.admin.util.Pager;

@ParentPackage("struts-default")
@Namespace("")
@InterceptorRef("defaultStack")
@Results( {
		@Result(name = "success", location = "/WEB-INF/admin/main.html"),
		@Result(name = "quit", location = "/WEB-INF/admin/send.jsp"),
		@Result(name = "userinfo", location = "/WEB-INF/admin/user/UserInfo.jsp"),
		@Result(name = "inputuser", location = "/WEB-INF/admin/user/addUser.jsp"),
		@Result(name = "initedit", location = "/WEB-INF/admin/user/EditUser.jsp"),
		@Result(name = "respwd", location = "/WEB-INF/admin/user/ResetPassword.jsp")
})
public class UserAction extends BaseAction{
	
	private static final long serialVersionUID = -1693523335512741257L;
	
	@Resource
	private UserService userService;

	private long PwdResult;
	
	/***
	 * 登陆  /admin/user/login
	 * @return
	 */
	@Action("/admin/user/login")
	public String login() {
		try {

			HttpSession ses = request.getSession();

			String username = request.getParameter("userId");
			String pwd = request.getParameter("userPwd");
			List<KsUser> list = userService.GetKsUser(username,pwd);
			KsUser ksuser = null;
			if (list.size() > 0) {
				ksuser = (KsUser) list.get(0);
				if (username.equals(ksuser.getUsername())&& pwd.equals(ksuser.getKsPwd())) {
					ses.setAttribute("userinfo", username);
					return "success";
				}
			}

		} catch (Exception e) {
			return "quit";
		}
		return "quit";
	}
	
	/***
	 * 退出  /admin/user/quit
	 * @return
	 */
	@Action("/admin/user/quit")
	public String quit() {
		HttpSession ses = request.getSession();
		ses.removeAttribute("userinfo");
		return "quit";
	}

	/***
	 * 查询用户页面
	 * @return
	 */
	@Action("/admin/user/inputuser")
	public String inputuser() {
		
		List<KsUser> querylist=userService.getKsUser();
		request.setAttribute("list_UserInfo", querylist);	
		return "inputuser";
	}

	@Action("/admin/user/eidtinfo")
	public String eidtinfo() {

		return "eidtinfo";
	}
	
	/***
	 * 删除用户  传uid
	 * @return
	 */
	@Action("/admin/user/deleteuser")
	public String deleteuser() {
		String username = request.getParameter("uid");
		KsUser ksuser = userService.GetKsUser(username).get(0);
		userService.deleteKsUser(ksuser, request.getSession().getAttribute(
				"userinfo").toString());
		return userinfo();
	}
	/***
	 * 添加新用户
	 * @return
	 */
	@Action("/admin/user/saveuser")
	public String saveuser() {
		String ksCuserName = request.getParameter("userinfo_ksCuserName");
		String username = request.getParameter("userinfo_username");
		String pwd = request.getParameter("userinfo_ksPwd");
		String userno = request.getParameter("userinfo_ksUserNo");
		String usergroup = request.getParameter("userinfo_ksUserGroup");
		String identity = request.getParameter("userinfo_ksIdentify");
		String status = request.getParameter("userinfo_ksStatus");

		KsUser ksuser = new KsUser();
		ksuser.setKsCreateTime(new SimpleDateFormat("yyyy-MM-dd")
				.format(new Date()));
		ksuser.setKsCuserName(ksCuserName);
		ksuser.setKsIdentify(identity);
		ksuser.setKsPwd(pwd);
		ksuser.setKsUserNo(userno);
		ksuser.setKsUserGroup(usergroup);
		ksuser.setKsStatus(status);
		ksuser.setUsername(username);
		HttpSession ses = request.getSession();

		userService.addKsUser(ksuser, ses.getAttribute("userinfo").toString());

		return userinfo();
	}
	/***
	 * 修改用户
	 * @return
	 */
	@Action("/admin/user/edituser")
	public String edituser() {
		String ksCuserName = request.getParameter("userinfo_ksCuserName");
		String username = request.getParameter("userinfo_username");
		String pwd = request.getParameter("userinfo_ksPwd");
		String userno = request.getParameter("userinfo_ksUserNo");
		String usergroup = request.getParameter("userinfo_ksUserGroup");
		String identity = request.getParameter("userinfo_ksIdentify");
		String status = request.getParameter("userinfo_ksStatus");
		
		KsUser ksuser = userService.GetKsUser(request.getParameter("userinfo_ksUserId")).get(0);

		ksuser.setKsCreateTime(new SimpleDateFormat("yyyy-MM-dd")
				.format(new Date()));
		ksuser.setKsCuserName(ksCuserName);
		ksuser.setKsIdentify(identity);
		ksuser.setKsPwd(pwd);
		ksuser.setKsUserNo(userno);
		ksuser.setKsUserGroup(usergroup);
		ksuser.setKsStatus(status);
		ksuser.setUsername(username);
		userService.updateKsUser(ksuser, request.getSession().getAttribute(
				"userinfo").toString());
		return userinfo();

	}
	/***
	 * 查询用户   修改
	 * @return
	 */
	@Action("/admin/user/initedit")
	public String initedit() {
		List<KsUser> querylist0 = userService.getKsUser();
		request.setAttribute("list_UserInfo", querylist0);

		String id = request.getParameter("uid");
		List<KsUser> querylist = userService.GetKsUser(id);
		if (querylist.size() > 0) {
			KsUser userinfo = querylist.get(0);
			request.setAttribute("userinfo", userinfo);
			request.setAttribute("username", userinfo.getUsername());
		}
		return "initedit";
	}

	@Action("/admin/user/respwd")
	public String respwd() {
		try {

			String id = request.getSession().getAttribute("userinfo")
					.toString();
			KsUser ksuer = (KsUser) userService.GetKsUser(id,"").get(0);
			request.setAttribute("ksuer", ksuer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "respwd";
	}
	/***
	 * 修改用户密码
	 * @return
	 */
	@Action("/admin/user/upwd")
	public String upwd() {
		try {

			PwdResult = 0;
			List<KsUser> lu = userService.GetKsUser(request
					.getParameter("userinfo_ksUserId"),"");
			String userinfo_Oripassword = request
					.getParameter("userinfo_Oripassword");
			if (lu.size() > 0) {
				KsUser uu = lu.get(0);
				if (userinfo_Oripassword.trim().equals(uu.getKsPwd().trim())) {
					uu.setKsPwd(request.getParameter("userinfo_ksPwd"));

					PwdResult = userService.updateKsUser(uu, request
							.getSession().getAttribute("userinfo").toString());

				} else {
					PwdResult = -1;

					return respwd();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respwd();

	}
	/***
	 * 查询所有用户
	 * @return
	 */
	@Action("/admin/user/userinfo")
	public String userinfo() {
		try {

			String pno = request.getParameter("pno") == null ? "1" : request
					.getParameter("pno").toString().trim();
			String rid = request.getParameter("uiid");
			Pager pager = new Pager();
			int iPageNo = 1;
			try {
				iPageNo = Integer.parseInt(pno);
			} catch (Exception e) {
			}
			pager.setCntOfPage(20);
			pager.setCurPage(iPageNo);
			List<KsUser> querylist = userService.GetKsUserList(rid, pager);
			request.setAttribute("list_UserInfo", querylist);
			request.setAttribute("PAGER", pager);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "userinfo";
	}


	public long getPwdResult() {
		return PwdResult;
	}

	public void setPwdResult(long pwdResult) {
		PwdResult = pwdResult;
	}
}
